package com.qq.afterSaleSystem.dao;

import com.qq.afterSaleSystem.entity.Apply;
import com.qq.afterSaleSystem.entity.ShopMaintain;
import com.qq.afterSaleSystem.entity.MaintainMoney;
import com.qq.afterSaleSystem.entity.Part;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface ShopMaintainMapper {
    @Select("Select * From car_maintain where state=1")
    List<ShopMaintain> getAll1();

    @Select("select maintain_type,count(*)as count from car_maintain where state=1 group by maintain_type")
    List<ShopMaintain> getNumOfMaintainTypeAndDo();

    @Select("select person,count(*)as count from car_maintain where state=1 group by person")
    List<ShopMaintain> getNumOfMaintainPerson();

    int addShopMaintain(ShopMaintain cmt);

    @Select("select id,car_number,get_time,maintain_type,plate_number,car_type,color,vin_num,engine_number," +
            "person,state,job_num,car_allmoney,push_money,over_time from car_maintain where id=#{id}")
    ShopMaintain shopMaintainById(Integer id);

    @Update("update car_maintain set car_number=#{carNumber},maintain_type=#{maintainType},plate_number=#{plateNumber}," +
            "car_type=#{carType},color=#{color},vin_num=#{vinNum},engine_number=#{engineNumber},person=#{person}," +
            "job_num=#{jobNum},car_allmoney=#{carAllmoney},push_money=#{pushMoney},over_time=#{overTime} where id=#{id}")
    int updateShopMaintain(ShopMaintain cmt);

    @Delete("delete from car_maintain where id=#{id}")
    int deleteShopMaintain(Integer id);

    @Select("select id,part_var,part_name from part where part_total>0")
    List<Part> getPart();

    @Insert("insert into apply_for(parts_name,time,proposer,car_id,state) values(#{partsName},#{time},#{proposer},#{carId},#{state})")
    int insertApply(Apply ap);

    @Select("select * from apply_for where car_id=#{id} and state=0")
    List<Apply> getApplyByCarId(Integer id);

    @Select("select * from apply_for where state=0")
    @Results({
            @Result(column="id",property="id"),
            @Result(column="parts_name",property="partsName"),
            @Result(column="time",property="time"),
            @Result(column="proposer",property="proposer"),
            @Result(column="car_id",property="carId"),
            @Result(column="state",property="state"),
            @Result(column="car_id",property="cmt",one=@One(select="com.qq.afterSaleSystem.dao.ShopMaintainMapper.getCard",fetchType= FetchType.EAGER))
    })
    List<Apply> getAllApply();
/*getCard查询，是为了getAllApply联立一对一查询*/
    @Select("select * from car_maintain where id = #{id} ")
    ShopMaintain getCard(Integer id);


    @Delete("delete from apply_for where id=#{id}")
    int deleteApply(Integer id);

    @Update("update apply_for,part set apply_for.state=1,part.part_total=part.part_total-1 where apply_for.id=#{id} and part.part_total>0 and part.part_name=#{partsName}")
    int updateApplyStateTo1(Integer id, String partsName);

    @Select("select * from apply_for where state=1")
    @Results({
            @Result(column="id",property="id"),
            @Result(column="parts_name",property="partsName"),
            @Result(column="time",property="time"),
            @Result(column="proposer",property="proposer"),
            @Result(column="car_id",property="carId"),
            @Result(column="state",property="state"),
            @Result(column="car_id",property="cmt",one=@One(select="com.qq.afterSaleSystem.dao.ShopMaintainMapper.getCard",fetchType= FetchType.EAGER))
    })
    List<Apply> getAllApplyStateIs1();

    @Insert("insert into maintain_money(parts_name,time,car_id) values(#{partsName},#{time},#{id})")
    int insertMaintainMoney(Integer id, String partsName, String time);

    @Select("select * from maintain_money where car_id=#{id}")
    List<MaintainMoney> getMaintainMoneyByCarId(Integer id);

    @Update("update maintain_money set parts_money=#{partsMoney},maintain_money=#{maintainMoney}," +
            "ingredients_money=#{ingredientsMoney},all_money=#{allMoney}" +
            " where id=#{id}")
    int updateMoneyById(MaintainMoney money);

    @Select("select * from maintain_money where id=#{id}")
    MaintainMoney getMoneyById(Integer id);
}
